package com.ruoyi.web.controller.community;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.community.domain.*;
import com.ruoyi.community.mapper.EcFoodsInOrderMapper;
import com.ruoyi.community.mapper.EcOrdersMapper;
import com.ruoyi.community.mapper.HelpCommentMapper;
import com.ruoyi.community.mapper.HelpGoodsMapper;
import com.ruoyi.community.utils.MessageUtil;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/community/help")
public class HelpGoodsController extends BaseController {

    @Autowired
    HelpGoodsMapper helpGoodsMapper;

    @Autowired
    EcOrdersMapper ecOrdersMapper;

    @Autowired
    HelpCommentMapper helpCommentMapper;

    @Autowired
    private ISysUserService userService;

    @Autowired
    EcFoodsInOrderMapper ecFoodsInOrderMapper;

    @GetMapping("/goods")
    public TableDataInfo getHelpGoods()
    {
        startPage();
        List<HelpGoods> list = helpGoodsMapper.selectAllGoodsByCommunityId(SecurityUtils.getCommunityId());
        return getDataTable(list);
    }

    @PutMapping("/good")
    public AjaxResult updateHelpGood(@RequestBody HelpGoods helpGood) {
        return toAjax(helpGoodsMapper.updateByPrimaryKey(helpGood));
    }

    @PutMapping("/goodNotice")
    public AjaxResult updateHelpGoodNotice(@RequestBody HelpGoods helpGood) {
        List<Integer> oids = ecFoodsInOrderMapper.selectOrderIdsByFoodId(helpGood.getGoodsId());
        List<Long> uids = new ArrayList<>();
        for (int i = 0; i < oids.size(); i++) {
            EcOrders temp = ecOrdersMapper.selectEcOrdersById(oids.get(i).longValue());
            if (temp.getRestaurantId() == 0) {
                uids.add(temp.getUserId());
            }
        }
        if (uids.size() != 0) {
            Announce announce = new Announce();
            announce.setContent("您购买的帮购商品已到货，请取货");
            announce.setType(AnnounceType.TONGYONG);
            System.out.println("uid:" + uids.size());
            // uids为空时报错
            MessageUtil.addAnnounceList(announce, uids);
        }
        System.out.println("help:" + helpGood.toString());
        return toAjax(helpGoodsMapper.updateByPrimaryKey(helpGood));
    }

    @PostMapping("/good")
    public AjaxResult addHelpGood(@RequestBody HelpGoods helpGood) {
        helpGood.setCommunityId(SecurityUtils.getCommunityId());
        return toAjax(helpGoodsMapper.insert(helpGood));
    }

    @GetMapping("/orders")
    public TableDataInfo getHelpOrders(EcOrders ecOrders)
    {
        startPage();
        ecOrders.setRestaurantId(new Long(0));
        List<EcOrders> list = ecOrdersMapper.selectEcOrdersList(ecOrders);
        return getDataTable(list);
    }

    @GetMapping("/comments")
    public TableDataInfo getHelpComments(HelpComment helpComment)
    {
        startPage();
        List<HelpComment> helpCommentList = helpCommentMapper.selectByGoodsId(helpComment.getGoodsId());
        for(int i = 0; i < helpCommentList.size(); i++) {
            HelpComment h = helpCommentList.get(i);
            Long userId = h.getUserId();
            SysUser u = userService.selectUserById(userId);
            h.setUserName(u.getUserName());
            h.setUserPicture(u.getAvatar());
        }
        return getDataTable(helpCommentList);
    }

}
